package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.RentPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.BusRent;
import com.southwind.mapper.BusCarMapper;
import com.southwind.mapper.BusCustomerMapper;
import com.southwind.service.BusRentService;
import com.southwind.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/rent")
public class BusRentController {

    @Autowired
    private BusCustomerMapper customerMapper;
    @Autowired
    private BusRentService rentService;
    @Autowired
    private BusCarMapper carMapper;

    @GetMapping("/initRentFrom")
    public BusRent initRentFrom(BusRent rent){
        rent.setRentid(RandomUtils.createRandomStringUseTime("CZ"));
        rent.setBegindate(new Date());
        rent.setOpername(this.customerMapper.getNameByIdentity(rent.getIdentity()));
        return rent;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody BusRent rent){
        rent.setRentflag(0);
        boolean save = this.rentService.save(rent);
        this.carMapper.rent(rent.getCarnumber());
        if(save) return ResultObj.RENT_ADD_SUCCESS;
        return ResultObj.RENT_ADD_ERROR;
    }

    @GetMapping("/load")
    public Map load(RentPageModel rentPageModel){
        Page<BusRent> page = new Page<>(rentPageModel.getPage(), rentPageModel.getLimit());
        QueryWrapper<BusRent> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(rentPageModel.getRentid()), "rentid", rentPageModel.getRentid())
                    .eq(rentPageModel.getRentflag() != null, "rentflag",rentPageModel.getRentflag());
        Page<BusRent> resultPage = this.rentService.page(page, queryWrapper);
        return ResultObj.resultData(resultPage.getRecords(), resultPage.getTotal());
    }

}

